#include "Engine.h"
#include <stdexcept>
#include <iostream>
#include "windows.h"
#include "windowsx.h"

using namespace std;
namespace SEngine
{
	
	LRESULT CALLBACK WndProc(HWND, UINT, WPARAM,LPARAM);

	Engine::Engine(void)
	{
		
		
	}	
	
	Engine::~Engine(void)
	{
	}
	
	void Engine::Run()
	{
		#if DEBUG_PROCESS
			cout<< "Create main window"<< endl;
		#else
			ShowWindow(GetConsoleWindow(),SW_HIDE);
		#endif
		
		int result=CreateEngineMainWindow();
		
		if(result)
		{
	    #if DEBUG_PROCESS
		  cout<< "do messaging"<< endl;
		#endif
		MSG msg; 
		while(GetMessage(&msg,NULL,0,0))
		{
			Update();
			Draw();
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		}
		else
		{
		   #if DEBUG_PROCESS
		  cout<< "Error create Window"<< endl;
		#endif
		}
		#if DEBUG_PROCESS
		  cout<< "now stoped"<< endl;
		#endif

		//return msg.wParam;
	}
	int Engine::CreateEngineMainWindow()
	 {
		HWND hwnd;
		WNDCLASS w;
		int result=0;
		memset(&w,0,sizeof(WNDCLASS));
		w.style = CS_HREDRAW | CS_VREDRAW;
		w.lpfnWndProc = WndProc;
		HINSTANCE hInstance=GetModuleHandle(NULL);
		w.hInstance = hInstance;
		w.hbrBackground = GetStockBrush(WHITE_BRUSH);
	    char szTmp[256] = {"\0"};
	    GetModuleFileName(hInstance, szTmp, sizeof(szTmp));
		w.lpszClassName = "C Windows";
		RegisterClass(&w);
		//typeid(*this).name()
		hwnd = CreateWindow("C Windows",szTmp, WS_OVERLAPPEDWINDOW,
			10,10,600,480,NULL,NULL,hInstance,NULL);
		result= ShowWindow(hwnd,SW_SHOW);
		if(!result)
		{
		result =UpdateWindow(hwnd);
		
		}
		 Frames=0;
		return result;
		
	 }
	void Engine::Update()
	{
		cout<<"Udpate base"<<endl;
	}
	int Engine::doFrame()
	{
	//return 0;	 
 Frames++;
return Frames;
 
	}
	void Engine::Draw()
	{
		
		cout<<"Draw base  Frames count="<< doFrame() <<endl;
	}
	LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wparam,LPARAM lparam)
	{
		if (Message == WM_DESTROY )
		{
			PostQuitMessage(0);
			return 0;
		}
		return DefWindowProc(hwnd,Message,wparam,lparam);
	}
}
